<template>
  <van-icon v-if="value===1" name="good-job" @click="changesttitude"></van-icon>
  <van-icon v-else name="good-job-o" @click="changesttitude"></van-icon>
</template>

<script>
import { toLike, cancelLike } from '@/api/user.js'
export default {
  name: 'Attitude',
  props: {
    value: {
        type: Number,
        required: true,
        default: 0
    },
    articleId: {
      type: [Object, String, Number],
      default: null
    }
  },
  data() {
      return {
      }
  },
  created() {
    // console.log(this.articleId);
  },
  updated() {
    // console.log(this.articleId);
  },
  methods: {
      async changesttitude() {
        this.$toast.loading({
          duration: 0, // 持续展示 toast
          message: '操作中...',
          forbidClick: true // 是否禁止背景点击
      })
        try {
          if (this.value === 1) {
          // 已点赞，要取消
            await cancelLike(this.articleId)
            this.$toast.success('取消成功')
          } else {
          // 未点赞，要赞
            await toLike(this.articleId)
            this.$toast.success('点赞成功')
          }
          this.$emit('input', this.value === 1 ? 0 : 1)
        } catch (error) {
          this.$toast.fail('操作失败')
        }
      }
  }
}
</script>

<style scope lang="less">
.van-icon-good-job {
  color: orange !important;
}
</style>
